import Toast from '../../../component/Toast';
import { uuid } from '../../../util/code'
import { clone } from '../../../util/utils'
import { listChannel, saveChannel, removeChannel } from '../service/channelService';

const defaultState = {
	
	list: [],
	
}

export default {

	namespace: 'channel',

	state: {...defaultState},

	reducers: {
		
		reset(state, {tab}) {
			return {...defaultState}
		},
		
		setList(state, {list}) {
			return {...state, ...{list}}
		},
		
	},
	
	effects: {
		
		*flushRecord({record, key}, {select, put}) {
			let {list} = yield select(state => state.channel)
			// save时使用uuid的key
			key = key || record.key
			record.key = record.channel
			list = clone(list);
			for (let i = 0; i < list.length; i ++) {
				if (list[i] && list[i].key == key) {
					list.splice(i, 1, record)
					yield put({type: 'setList', list})
					return
				}
			}
		},
		
		*listChannel({pageNum, pageSize}, {select, put}) {
			let res = yield listChannel()
			if (res.code == 1) {
				let list = res.data
				if (list) {
					// 添加key字段
					for (let item of list) {
						if (!item.key)
							item.key = item.channel
					}
					yield put({type: 'setList', list})
				}
			} else {
				res.msg && Toast.show(res.msg)
			}
		},
		
		*editRecord({record, key, value}, {select, put}) {
			let {list} = yield select(state => state.channel)
			let channel = findListByKey(list, record.key)
			if (channel) {
				channel[key] = value
				yield put({type: 'setList', list})
			}
		},
		
		*cancel({record}, {select, put}) {
			let {list, editBuf} = yield select(state => state.channel)
			list = clone(list)
			if (list && list.length > 0) {
				let channel
				let i = 0
				for (; i < list.length; i ++) {
					if (list[i] && list[i].key == record.key) {
						channel = list[i]
						break
					}
				}
				if (channel) {
					// 新建的record
					// 删除
					list.splice(i, 1);
					yield put({type: 'setList', list})
				}
			}
		},
		
		*save({key}, {select, put}) {
			let {list, editBuf, total} = yield select(state => state.channel)
			let record = findListByKey(list, key)
			if (record) {
				let res
				if (!record.channel) {
					return Toast.show('通道值不能为空')
				}
				if (record.strategy == undefined) {
					return Toast.show('必须选择一个通道策略')
				}
				record.channelName = record.channel
				res = yield saveChannel(record)
				if (res.code == 1) {
					yield put({type: 'setTotal', total: total + 1})
				}
				if (res.code == 1 && res.data) {
					// 局部刷新
					yield put({type: 'flushRecord', record: res.data, key})
				} else {
					Toast.show(res.msg)
				}
			}
		},
		
		*add(action, {select, put}) {
			let {list} = yield select(state => state.channel)
			list = clone(list)
			if (list) {
				list.splice(0, 0, {key: uuid(), editable: true})
				yield put({type: 'setList', list})
			}
		},
		
		*remove({channel}, {select, put}) {
			let res = yield removeChannel({channelName: channel})
			if (res.code == 1) {
				let {list} = yield select(state => state.channel)
				if (list && list.length > 0) {
					for (let i = 0; i < list.length; i ++) {
						if (list[i] && list[i].key == channel) {
							list.splice(i, 1)
							yield put({type: 'setList', list})
							break
						}
					}
				}
			} else {
				Toast.show(res.msg)
			}
		},
		
	}

}

function findListByKey(list, key) {
	if (list && list.length > 0) {
		for (let i = 0; i < list.length; i ++) {
			if (list[i] && list[i].key == key)
				return list[i]
		}
	}
}

function getrealLen(list) {
	let len = 0
	for (let item of list) {
		if (item.channel) 
			len ++;
	}
	return len
}
